﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

using umbraco.cms.businesslogic.datatype;

namespace DigibizAdvancedMediaPicker
{

    public class DAMP_Data : umbraco.cms.businesslogic.datatype.DefaultData
    {
        /// <summary>
		/// Initializes a new instance of the <see cref="XmlData"/> class.
		/// </summary>
		/// <param name="dataType">Type of the data.</param>
        public DAMP_Data(BaseDataType dataType)
			: base(dataType)
		{
		}

		/// <summary>
		/// Converts the data value to XML.
		/// </summary>
		/// <param name="data">The data to convert to XML.</param>
		/// <returns></returns>
		public override XmlNode ToXMl(XmlDocument data)
		{
			// check that the value isn't null and starts with an opening angle-bracket.
			if (this.Value != null && !string.IsNullOrEmpty(this.Value.ToString()) && this.Value.ToString().StartsWith("<"))
			{
                try
                {
                    // load the value into an XML document.
                    XmlDocument xd = new XmlDocument();
                    xd.LoadXml(this.Value.ToString());

                    // return the XML node.
                    return data.ImportNode(xd.DocumentElement, true);
                }
                catch (Exception ex)
                {
                    DAMP_Helper.LogException(ex);
                }
			}
			
			// otherwise render the value as default (in CDATA)
			return base.ToXMl(data);
		}
    }
}